-- 实现长按事件的节点
local LongTouch = {}
-- 触发时间，按多长时间触发
local mTriggerTime = 0.5
-- 可抖动区域，如果在长按触发前移动超过这个距离则不会触发事件
local removableDis   = 10

-- 类方法touchnode -- 给谁添加这个事件   longPressHandler -- 长按触发回调方法 
--clickHandler 还可以添加单击事件（touchIn）可设定触发时间 triggerTime
LongTouch.registerLongPressHandler = function(touchnode, longPressHandler, clickHandler, triggerTime)
    triggerTime = triggerTime or mTriggerTime
    
    -- 记录长按时间
    local countTime = nil
    -- 是用scheduler 来计时的，记录scheduler 的id
    local sid       = nil
    -- 开始位置
    local beganPoint= nil
    -- 有没有回调过
    local haveCall  = false
    -- 有没移动（超出可移动范围）
    local isMoved   = false
    -- 有没已添加过监听
    if touchnode.m_listenner_initFlag then
        return
    end
    -- 计时方法
    local function countTimeFunc( delay )
        countTime = countTime or 0
        countTime = countTime + delay
        if countTime > triggerTime then
            -- 回调
            if longPressHandler then
                longPressHandler(touchnode, "longPressBegin")
            end
            
            -- 停止计时
            local scheduler = cc.Director:getInstance():getScheduler()
            scheduler:unscheduleScriptEntry(sid)
            sid       = nil
            countTime = nil
            haveCall  = true
        end
    end
    
    -- 触摸开始
    local function onTouchBegin(touch, event)
        LongTouch.isCancel = false
        if touchnode:isVisible() and cc.rectContainsPoint(touchnode:getBoundingBox(), 
            touchnode:getParent():convertTouchToNodeSpace(touch)) then
            
            beganPoint = touch:getLocation()
            -- 开始计时
            if longPressHandler then
                local scheduler = cc.Director:getInstance():getScheduler()
                sid = scheduler:scheduleScriptFunc( function ( delay )
                    countTimeFunc(delay)
                end, 0.1, false)
            end
            
            return true
        else
            return false
        end
    end


    -- 触摸移动
    local function onTouchMoved( touch, event )
        if haveCall == false and sid then
            local location = touch:getLocation()
            local width = location.x - beganPoint.x
            local height = location.y - beganPoint.y
            -- 停止计时
            if math.abs(width) > removableDis or math.abs(height) > removableDis then
                local scheduler = cc.Director:getInstance():getScheduler()
                scheduler:unscheduleScriptEntry(sid)
                sid       = nil
                countTime = nil
                
                isMoved   = true
            end
        end
    end


    -- 触摸结束
    local function onTouchEnded( touch, event )
        if sid then
            local scheduler = cc.Director:getInstance():getScheduler()
            scheduler:unscheduleScriptEntry(sid)
            sid = nil
        end
        if not LongTouch.isCancel then
            if haveCall then
                if longPressHandler then
                    longPressHandler(touchnode, "longPressEnded")
                end
            else
                if isMoved == false and clickHandler then
                    clickHandler(touchnode)
                end
            end
        end
        
        beganPoint=nil
        haveCall = false
        isMoved  = false
    end
    
    local function onTouchCancel(touch,event)
        LongTouch.isCancel = true
        onTouchEnded(touch, event)       
    end
    
    -- 添加监听
    local listener = cc.EventListenerTouchOneByOne:create()
    listener:setSwallowTouches(false)
    listener:registerScriptHandler(onTouchBegin, cc.Handler.EVENT_TOUCH_BEGAN)
    listener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED)
    listener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED)
    listener:registerScriptHandler(onTouchCancel,cc.Handler.EVENT_TOUCH_CANCELLED)

    
    local eventDispatcher = cc.Director:getInstance():getEventDispatcher()
    eventDispatcher:addEventListenerWithFixedPriority(listener, -1)
    --touchnode:getEventDispatcher():addEventListenerWithSceneGraphPriority(listener,touchnode)

    local function onNodeEvent(event)
        if event == 'exit' then
            touchnode:getEventDispatcher():removeEventListener(listener)
        end     
    end
    touchnode:registerScriptHandler(onNodeEvent) 

    touchnode.m_listenner_initFlag = true
    return touchnode
end

-- 取消事件
LongTouch.isCancel = false


return LongTouch